const cityData = [
  {
    id: "axzx",
    name: "广东省",
    children: [
      {
        id: "sdsd",
        name: "深圳市",
        children: [
          {
            id: "45dss",
            name: "南山区",
          },
          {
            id: "sdsd11",
            name: "福田区",
            children: [
              {
                id: "ddrr2",
                name: "A街道",
              },
            ],
          },
        ],
      },
      {
        id: "2323d",
        name: "东莞市",
        children: [
          {
            id: "xxs2",
            name: "A区",
          },
          {
            id: "kklio2",
            name: "B区",
          },
        ],
      },
    ],
  },
];

//输入id 找出 名称
// 例如 sdsd11 广东省深圳市南山区福田区

// function fn(id, cityData) {
//   cityData.forEach((item) => {});
// }


// const recursion = (cityData, id, name) => {
//   if (!cityData || !cityData.length) return;
//   for (let i = 0, len = cityData.length; i < len; i++) {

//     if (cityData[i].id === id) {
//       name += cityData[i].name
//       console.log('name = ', name);
//       return ;
//     }

//     const childs = cityData[i].children;
//     if (childs && childs.length > 0) {
//       recursion(childs, id, name + cityData[i].name);
//     }

//     console.log('iiii ====== ', cityData[i].name);

//   }

// };



const recursion2 = (cityData, id) => {
  if (!cityData || !cityData.length) return;
  for (let i = 0, len = cityData.length; i < len; i++) {

    if (cityData[i].id === id) {
      name += cityData[i].name
      console.log('name = ', name);
      return;
    }

    const childs = cityData[i].children;
    if (childs && childs.length > 0) {
      recursion2(childs, id, name + cityData[i].name);
    }

    // console.log('iiii ====== ', cityData[i].name);

  }

};









var name = "";
const recursion1 = (cityData, id) => {
  if (!cityData || !cityData.length) return;
  for (let i = 0, len = cityData.length; i < len; i++) {
    var str = cityData[i].name
    if (cityData[i].id === id) {
      name += str

      return name;
    }

    const childs = cityData[i].children;

    if (childs && childs.length > 0) {
      recursion1(childs, id);
    }

    if (i == cityData.length - 1) {
      str = "";
    }
    name += str;
  }
};

recursion1(cityData, 'sdsd11');
console.log("结果", name)
